package main

import (
	"fmt"
	"os"
)

type student struct {
	id    int
	name  string
	class string
}

// 学员列表
type studentMar struct {
	allStudents []*student
}

// 构造函数
func newStudentMar() *studentMar {
	return &studentMar{allStudents: make([]*student, 0, 100)}
}

// 添加
func (s *studentMar) addStudent(newStu *student) {
	s.allStudents = append(s.allStudents, newStu)
}

// 编辑
func (s *studentMar) editStudent(newStu *student) {
	for i, v := range s.allStudents {
		if v.id == newStu.id {
			s.allStudents[i] = newStu
			return
		}
	}
	fmt.Println("输入的学生找不到")
}

// 展示
func (s *studentMar) showStudent() {
	for _, v := range s.allStudents {
		fmt.Printf("学号：%d，名字:%s，班级:%s\n", v.id, v.name, v.class)
	}
}

// student 类型的够造函数
func newStudent(id int, name, class string) *student {
	return &student{id, name, class}
}

func showMenu() {
	fmt.Println("欢迎来到学院信息管理系统")
	fmt.Println("1. 添加学院")
	fmt.Println("2. 编辑学院")
	fmt.Println("3. 展示学院")
	fmt.Println("4. 退出")
}

func getInput() *student {
	var (
		id    int
		name  string
		class string
	)
	fmt.Println("请按要求输入学院信息;")
	fmt.Scanf("%d\n", &id)
	fmt.Scanf("%s\n", &name)
	fmt.Scanf("%s\n", &class)
	stu := newStudent(id, name, class)
	return stu
}

func main() {
	for {
		showMenu()
		var input int
		fmt.Print("请输入你要操作的序号：")
		// 扫描用户输入，赋值给input
		fmt.Scanf("%d\n", &input)
		sm := newStudentMar()
		switch input {
		case 1:
			stu := getInput()
			sm.addStudent(stu)
		case 2:
			stu := getInput()
			sm.editStudent(stu)
		case 3:
			sm.showStudent()
		case 4:
			os.Exit(0)
		default:
			os.Exit(0)
		}
	}
}
